package com.xbongbong.paas.toolbox.wrap;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.exception.XbbRuntimeException;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ErrorCode;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

/**
 * 返回数据封装类
 * @param <T> 返回数据VO数据
 *
 * @author kaka
 * @time 2018-7-30 15:43
 */
public class XbbResponse<T> extends ResultVO<T> implements Serializable {

    private static final long serialVersionUID = -5773353575458855532L;

    private String xssCode;

    public XbbResponse() {
    }

    public XbbResponse(ResultVO<T> resultVO) {
        super.code = resultVO.getCode();
        super.msg = resultVO.getMsg();
        super.setResult(resultVO.getResult());
        super.setTotalCount(resultVO.getTotalCount());
        super.setSuccess(resultVO.getCode().equals(ErrorCodeEnum.API_SUCCESS.getCode()));
    }

    public XbbResponse(Integer code, String message) {
        super.code = code;
        super.msg = message;
        super.setSuccess(code.equals(ErrorCodeEnum.API_SUCCESS.getCode()));
    }

    public XbbResponse(Integer code, String message, T dtat) {
        super.code = code;
        super.msg = message;
        super.setResult(dtat);
        super.setSuccess(code.equals(ErrorCodeEnum.API_SUCCESS.getCode()));
    }

    public XbbResponse(Integer code, String message, String xssCode) {
        super.code = code;
        super.msg = message;
        this.xssCode = xssCode;
        super.setSuccess(code.equals(ErrorCodeEnum.API_SUCCESS.getCode()));
    }

    public XbbResponse(ErrorCodeEnum errorCodeEnum) {
        this(errorCodeEnum.getCode(), errorCodeEnum.getMsg());
    }

    public XbbResponse(ErrorCode errorCode, String message) {
        this(errorCode.getCode(), message);
    }
    public XbbResponse(XbbException e) {
        this(e.getCode(), e.getMsg());
    }

    public XbbResponse(XbbRuntimeException e) {
        this(e.getCode(), e.getMsg());
    }

    public XbbResponse(ErrorCodeEnum errorCodeEnum, String message) {
        this(errorCodeEnum.getCode(), message);
    }

    public XbbResponse(T t) {
        this(ErrorCodeEnum.API_SUCCESS);
        super.setResult(t);
        if (t != null) {
            if (t instanceof Map) {
                super.setTotalCount(((Map) t).size());
            } else if (t instanceof Collection) {
                super.setTotalCount(((Collection) t).size());
            } else {
                super.setTotalCount(1);
            }
        }
    }

    public XbbResponse(T t, Integer count) {
        this(ErrorCodeEnum.API_SUCCESS);
        super.setResult(t);
        super.setTotalCount(count);
    }

    public String getXssCode() {
        return xssCode;
    }

    public void setXssCode(String xssCode) {
        this.xssCode = xssCode;
    }

    public XbbResponse(ErrorCode errorCode) {
        this(errorCode.getCode(), errorCode.getMsg());
    }
}
